package com.util;



import org.apache.commons.lang3.RandomUtils;

public class EncodeUtil {

    /**
    发送选测命令
     */
    public static  String combineMessageForSelectTest(int pcode,String stationGroup,int ecode) {
        StringBuilder sbcode = new StringBuilder();
        sbcode.append("33");//命令类型
        sbcode.append(String.format("%02X",Integer.parseInt(Integer.toHexString(pcode),16)));
        sbcode.append(String.format("%04X",Integer.parseInt(Integer.toHexString(ecode),16)));
        StringBuilder scode = new StringBuilder();
        scode.append("EB95");
        scode.append(String.format("%02X", sbcode.length() / 2));
        scode.append(stationGroup);
        scode.append("0041");
        scode.append("0000");
        scode.append("0000");
        scode.append(sbcode);
        scode.append("00");
        scode.append(String.format("%04X", RandomUtils.nextInt(1, 65533)));//"0000"
        scode.append(String.format("%04X", ByteUtil.buildCrc16(scode.toString())));
        return scode.toString();
    }

    /**
     * 发送控制命令
     */
    public static  String combineMessageForControl(int pcode,String stationGroup,int ecode,String status) {
        StringBuilder sbcode = new StringBuilder();
        sbcode.append("60");//命令类型
        sbcode.append(String.format("%02X",Integer.parseInt(Integer.toHexString(pcode),16)));
        sbcode.append(String.format("%04X",Integer.parseInt(Integer.toHexString(ecode),16)));
        sbcode.append("01");
        sbcode.append(status);
        StringBuilder scode = new StringBuilder();
        scode.append("EB95");
        scode.append(String.format("%02X", sbcode.length() / 2));
        scode.append(stationGroup);
        scode.append("0000");
        scode.append("0041");
        scode.append("0000");
        scode.append(sbcode);
        scode.append("00");
        scode.append(String.format("%04X", RandomUtils.nextInt(1, 65533)));//"0000"
        scode.append(String.format("%04X", ByteUtil.buildCrc16(scode.toString())));
        return scode.toString();
    }
}
